/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BUS;

import DAO.DiemHocTapDAO;
import DAO.MonHocDAO;
import DAO.QuyDinhDAO;
import POJO.DiemHocTapPOJO;
import POJO.HocSinhPOJO;
import POJO.MonHocPOJO;
import POJO.QuyDinhPOJO;
import java.util.ArrayList;

/**
 *
 * @author MinhHieu
 */
public class DiemHocTapBUS {

    public static int soHocSinhDat(String MaNamHoc, String MaLop, String HocKy, String MaMonHoc){
        int num = 0;
        ArrayList<HocSinhPOJO> HSList = HocSinhBUS.selectHocSinh_Lop(MaLop);
        QuyDinhPOJO QD = QuyDinhDAO.selectAll();
        for (int i = 0; i < HSList.size(); i++) {
            double DiemTBMon = DiemHocTapBUS.averageMon(HSList.get(i).getMaHS(), MaMonHoc, HocKy, MaNamHoc);
            if(DiemTBMon>QD.getDiemChuan()){
                num++;
            }
        }
        return num;
    }

    public static double averageNamHoc(String MaHocSinh, String MaNamHoc){
        double average = 0;
        double hk1 = DiemHocTapBUS.averageHK(MaHocSinh, "1", MaNamHoc);
        double hk2 = DiemHocTapBUS.averageHK(MaHocSinh, "2", MaNamHoc);
        if(hk1==-1 || hk2 ==-1){
            average=-1;
            return average;
        }
        average = (hk1+hk2*2)/3;
        return average;
    }

    public static double averageHK(String MaHocSinh, String HocKy, String MaNamHoc){
        double average = 0;
        int tongheso = 0;
        ArrayList<MonHocPOJO> MonHocList = MonHocDAO.selectAll();
        int size = MonHocList.size();
        for(int i=0;i<size;i++){
            String MaMonHoc = MonHocList.get(i).getMaMonHoc();
            int HeSo = MonHocList.get(i).getHeSo();
            double DiemTBMon = DiemHocTapBUS.averageMon(MaHocSinh, MaMonHoc, HocKy, MaNamHoc);
            if(DiemTBMon==-1){
                average = -1;
                return average;
            }
            average = average + DiemTBMon*HeSo;
            tongheso+=HeSo;
        }
        return average/tongheso;
    }

    public static double averageMon(String MaHocSinh,String MaMonHoc, String HocKy, String MaNamHoc){
        double average = 0;
        
        ArrayList<DiemHocTapPOJO> Diem = DiemHocTapDAO.LayDiemHoctap(MaHocSinh, MaMonHoc, HocKy, MaNamHoc);
        if(Diem.isEmpty()){
            average = -1;
            return average;
        }
        if(Diem.get(0).getDiem15()==null || Diem.get(0).getDiem1Tiet()==null || Diem.get(0).getDiemCuoiKy()==null){
            average = -1;
            return average;
        }
        double D15 = Double.parseDouble(Diem.get(0).getDiem15());
        double DT = Double.parseDouble(Diem.get(0).getDiem1Tiet());
        double DCK = Double.parseDouble(Diem.get(0).getDiemCuoiKy());
        average = (D15+DT*2+DCK)/4;

        return average;
    }

    public static  boolean  nhapDiemNhapDiem(String maHS,String maMH,String hocKi,String MaNamHoc,String  Diem15, String  Diem1tiet, String  DiemCKi){
        boolean kq = DiemHocTapDAO.NhapDiem(maHS, maMH, hocKi,MaNamHoc,Diem15,Diem1tiet,DiemCKi);
        return kq;
    }
    public static  boolean  suaDiem(String maHS,String maMH,String hocKi,String MaNamHoc,String  Diem15, String  Diem1tiet, String  DiemCKi){
        boolean kq = DiemHocTapDAO.SuaDiem(maHS, maMH, hocKi,MaNamHoc,Diem15,Diem1tiet,DiemCKi);
        return kq;
    }
    public static  ArrayList<DiemHocTapPOJO> laydiem(){
        ArrayList<DiemHocTapPOJO> list = DiemHocTapDAO.laydiemht();
        return list;
    }

    public static ArrayList<DiemHocTapPOJO> loaddulieudiembenNhapDiem(String MaHS,String MaMH,String hk,String manhoc ){
         ArrayList<DiemHocTapPOJO> list = DiemHocTapDAO.LayDiemHoctap(MaHS, MaMH, hk, manhoc);
        return list;

    }
}
